#!/bin/bash
# Copyright (c) Huawei Technologies Co., Ltd. 2010-2022. All rights reserved.
# description: the script that make install logAPI
# date: 2022-06-30
set -e

function build_logAPI()
{
    echo =========== compile log4cpp==================
    open_src_path=${SRC_DIR}/source/eSDK_OBS_API/eSDK_OBS_API_C++
    log4cpp_dir=${SRC_DIR}/platform/eSDK_LogAPI_V2.1.10/log4cpp
    logAPI_dir=${SRC_DIR}/platform/eSDK_LogAPI_V2.1.10/eSDKLogAPI
    log4cpplib_dir=${log4cpp_dir}/build

    cd ${log4cpp_dir}
    chmod 777 configure
    chmod 777 autogen.sh
    aclocal -I m4
    ./autogen.sh
    if [ ${ARCH} = "aarch64" ];then
        CXXFLAGS="-fstack-protector-all -Wl,-z,relro,-z,now -O2" ./configure --prefix=${log4cpplib_dir} --host=aarch64-linux-gnu --build=aarch64-gnu-linux --with-gnu-ld --with-pthreads
        lib_out=aarch64
    else
        CXXFLAGS="-fstack-protector-all -Wl,-z,relro,-z,now -O2" ./configure --prefix=${log4cpplib_dir} --with-pthreads
        lib_out=linux_64
    fi

    # disable rpath
    sed -i 's/hardcode_into_libs=yes/hardcode_into_libs=""/g' ./libtool
    sed -i 's/hardcode_libdir_flag_spec="\\\${wl}-rpath \\\${wl}\\\$libdir"/hardcode_libdir_flag_spec=""/g' ./libtool

    make clean
    make -j16
    make install

    cd ${open_src_path}

    mkdir -p ${logAPI_dir}/../C/${lib_out}
    cp -af ${log4cpplib_dir}/lib/liblog4cpp*.so* ${logAPI_dir}/../C/${lib_out}

    echo =========build the libeSDKLogAPI.so=========
    cd ${logAPI_dir}
    make clean
    make

    cp libeSDKLogAPI.so ../C/${lib_out} -f

    cd ${open_src_path}
}
